/**
 * @file:          CloseLoopV1_2.h
 * @brief:
 * @details:
 * @author:        wjh
 * @date created:  2023.06.09
 * @version:       1.0.0
 * @par copyright (c):
 *
 * @par history (desc):
 *   version:1.0.0, wjh, 2023.06.09,14:49:30
 */
#ifndef _CLOSELOOPV1_2_H_
#define _CLOSELOOPV1_2_H_

/* Include Files **************************************************************/
#include "Datatype.h"
#include "ch32v30x_module.h"

/* Macro Definition ***********************************************************/

/* Data Type Define ***********************************************************/

typedef struct CloseLoopV1_2
{
    /* public data */
    Mcu *mcu;

    float32 param_power_supply_V;
    float32 param_R_of_current_sample_ohm;
    float32 param_adc_ref_V;
    float32 param_amp_gain;
    int16 param_adc_calibration_buffer_size;
    float32 param_led_toggle_period_s;
    uint16 param_pwm_output_en;
    int16 param_current_dir;

    GpioGeneralType param_pin_LED_in;
    GpioGeneralType param_pin_K1_out;
    GpioGeneralType param_pin_K2_out;
    GpioGeneralType param_pin_K3_out;
    GpioGeneralType param_pin_UART_TX_out;
    GpioGeneralType param_pin_UART_RX_in;
    GpioGeneralType param_pin_STEP_in;
    GpioGeneralType param_pin_DIR_in;
    GpioGeneralType param_pin_EN_in;
    GpioGeneralType param_pin_SPI_CLK_out;
    GpioGeneralType param_pin_SPI_MISO_in;
    GpioGeneralType param_pin_SPI_MOSI_out;
    GpioGeneralType param_pin_SPI_CS_out;
    GpioGeneralType param_pin_IA_OP_O_out;
    GpioGeneralType param_pin_IB_OP_O_out;
    GpioGeneralType param_pin_SWC;
    GpioGeneralType param_pin_SWD;
    GpioGeneralType param_pin_RST;
    GpioGeneralType param_pin_PWM_IN_A1;
    GpioGeneralType param_pin_PWM_IN_A2;
    GpioGeneralType param_pin_PWM_IN_B1;
    GpioGeneralType param_pin_PWM_IN_B2;
    GpioGeneralType param_pin_PWM_DA_A;
    GpioGeneralType param_pin_PWM_DA_B;
    GpioGeneralType param_pin_TestGpio_out;
    GpioGeneralType param_pin_rotor_block_detect_out;

    float32 in_pwm_duty_A1;
    float32 in_pwm_duty_A2;
    float32 in_pwm_duty_B1;
    float32 in_pwm_duty_B2;
    
    int16 param_Ia_adc_offset;
    int16 param_Ib_adc_offset;
    int16 Ia_adc_value;
    int16 Ib_adc_value;
    uint32 led_toggle_period_count;
    uint32 led_toggle_count;
    // float32 in_pwm_da_A;
    // float32 in_pwm_da_B;
    // float32 in_led_en;
    // float32 out_IA;
    // float32 out_IB;
    // float32 out_key1_clicked;
    // float32 out_key2_clicked;
    // float32 out_key3_clicked;
    // float32 out_key4_clicked;


    /* private data */

    /* public method */
    void (*ConfigParam)(struct CloseLoopV1_2 *self);
    uint8 (*Init)(struct CloseLoopV1_2 *self);
    void (*LedToggle)(struct CloseLoopV1_2 *self);
    uint16 (*KeyRead)(struct CloseLoopV1_2 *self, uint16 gpio);
    uint8 (*AdcCalibration)(struct CloseLoopV1_2 *self);
    float32 (*Adc2Current)(struct CloseLoopV1_2 *self, int16 adc_val);
    float32 (*GetIaFdb)(struct CloseLoopV1_2 *self);
    float32 (*GetIbFdb)(struct CloseLoopV1_2 *self);
    void (*UpdatePWM)(struct CloseLoopV1_2 *self);

    /* private method */

} CloseLoopV1_2;

/* Variable Declare ***********************************************************/

/* Inline Function Define *****************************************************/

/* Function Declare ***********************************************************/
uint8 close_loop_v1_2_Init(CloseLoopV1_2 *self);
void close_loop_v1_2_LedToggle(CloseLoopV1_2 *self);
uint8 close_loop_v1_2_AdcCalibration(CloseLoopV1_2 *self);
float32 close_loop_v1_2_Adc2Current(CloseLoopV1_2 *self, int16 adc_val);
float32 close_loop_v1_2_GetIaFdb(CloseLoopV1_2 *self);
float32 close_loop_v1_2_GetIbFdb(CloseLoopV1_2 *self);
void close_loop_v1_2_UpdatePWM(CloseLoopV1_2 *self);

#endif